/**
 * tabBar页面路径列表 (用于链接跳转时判断)
 * tabBarLinks为常量, 无需修改
 */
const tabBarLinks = [
  'pages/index/index',
  'pages/category/index',
  'pages/flow/index',
  'pages/profile/profile'
];

// const trackUtil = require("/utils/trackUtil.js");
let queryOptions = {};
// 站点信息
import siteInfo from 'siteinfo.js';

App({
  onLaunch: function (data) {
    let App = this;
    // 设置api地址
    App.setApiRoot();
    queryOptions = data;
    // trackUtil.setTrackConfig();
    // const openId = wx.getStorageSync('open_id'); 
    // trackUtil.initWithOpenId(openId);
    // trackUtil.autoTrackAppLaunch(data);
  },
  onShow(data) {
    // trackUtil.autoTrackAppShow(data);
  },
  onHide() {
    // trackUtil.autoTrackAppHide(queryOptions);
  },

  globalData: {
    userInfo: null
  },
  
  api_root: '', // api地址
  /**
   * 设置api地址s
   */
  setApiRoot() {
    let App = this;
    App.api_root = `${siteInfo.siteroot}`;
  },

  /**
   * 获取小程序基础信息
   */
  getWxappBase(callback) {
    let App = this;
    // App._get('/api/setting', {}, result => {
    //   // 记录小程序基础信息
     
    //   callback && callback(wxapp);  
    // }, false, false);

    const wxapp = {top_background_color:'#E7A7B3',top_text_color:{text:'#fff',value:20},wxapp_title:'素材管理'}
    wx.setStorageSync('wxapp', wxapp);  
  },

  /**
  * 执行用户登录
  */
  doLogin() {
    // 保存当前页面
    let pages = getCurrentPages();
    if (pages.length) {
      let currentPage = pages[pages.length - 1];
      "pages/login/login" != currentPage.route &&
        wx.setStorageSync("currentPage", currentPage);
    }
    // 跳转授权页面
    wx.navigateTo({
      url: "/pages/login/login"
    });
  },

  /**
   * 当前用户id
   */
  getUserId() {
    return wx.getStorageSync('user_id') || 0;
  },

  /**
   * 显示成功提示框
   */
  showSuccess(msg, callback) {
    wx.showToast({
      title: msg,
      icon: 'success',
      success() {
        callback && (setTimeout(() => {
          callback();
        }, 1500));
      }
    });
  },

  /**
   * 显示失败提示框
   */
  showError(msg, callback) {
    wx.showModal({
      title: '友情提示',
      content: msg,
      showCancel: false,
      success(res) {
        // callback && (setTimeout(() => {
        //   callback();
        // }, 1500));
        callback && callback();
      }
    });
  },

  /**
   * get请求
   */
  _get(url, data, success, fail, complete, check_login) {
    let App = this;
    wx.showNavigationBarLoading();

    // 构造请求参数
    data = Object.assign({
    }, data);

    // if (typeof check_login === 'undefined')
    //   check_login = true;

    // 构造get请求
    let request = () => {
      data.token = wx.getStorageSync('token');
      wx.request({
        url: App.api_root + url,
        header: {
          'content-type': 'application/json',
          'Authorization': wx.getStorageSync('token')
        },
        data,
        success(res) {
          if (res.statusCode !== 200 || typeof res.data !== 'object') {
            console.log(res);
            App.showError('GET网络请求出错');
            return false;
          }
          console.log(res.statusCode,'-statusCode-')
          console.log(url,'---url')
          if(res.data.code === 102 && (url == '/mapi/user_operate/dolike' || url == '/mapi/user_operate/docollect')){
            wx.showModal({
              title: '温馨提示',
              content: '需登录后可操作点赞及收藏',
              success(res) {
                  if (res.confirm) {
                    wx.switchTab({
                      url: '/pages/profile/profile',
                    })
                  } else if (res.cancel) {
                      console.log('用户单击取消');
                  }
              }
          });
          
        }
          if (res.data.status === 400 || res.data.status === 401 || res.data.status === 402) {
              // App.doLogin(() => {
              //   App._post_form(url, data, success, fail);
              // });
              return false;
          } else if (res.data.status === 500 || res.data.status === 422) {
            App.showError(res.data.message);
            return false;
          } else {
            success && success(res.data);
          }
        },
        fail(res) {
          // console.log(res);
          App.showError(res.errMsg, () => {
            fail && fail(res);
          });
        },
        complete(res) {
          wx.hideNavigationBarLoading();
          complete && complete(res);
        },
      });
    };
    // 判断是否需要验证登录
    check_login ? App.doLogin(request) : request();
  },
  
  /**
   * post提交
   */
  _post_form(url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    let App = this;
    
    // 构造请求参数
    data = Object.assign({
    }, data);
    data.token = wx.getStorageSync('token')
    console.log(data,'data')
    wx.request({
      url: App.api_root + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': wx.getStorageSync('token')
      },
      method: 'POST',
      data,
      success(res) {
        if (res.statusCode !== 200 || typeof res.data !== 'object') {
          App.showError('POST网络请求出错');
          return false;
        }
        if (res.data.status === 400 || res.data.status === 401 || res.data.status === 402) {
          // 登录态失效, 重新登录
          App.doLogin(() => {
            App._post_form(url, data, success, fail);
          });
          return false;
        } else if (res.data.status === 500 || res.data.status === 422) {
          App.showError(res.data.message, () => {
            fail && fail(res);
          });
          return false;
        }
        success && success(res.data);
      },
      fail(res) {
        App.showError(res.message, () => {
          fail && fail(res);
        });
      },
      complete(res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        complete && complete(res);
      }
    });
  },


  /**
   * post 请求
   */

  _post(url, data, success, fail, complete) {
    wx.showNavigationBarLoading();
    let App = this;
    
    // 构造请求参数
    data = Object.assign({
    }, data);

    wx.request({
      url: App.api_root + url,
      method: 'POST',
      data,
      success(res) {
        if (res.statusCode !== 200 || typeof res.data !== 'object') {
          App.showError('POST网络请求出错');
          return false;
        }
        if (res.data.status === 400 || res.data.status === 401 || res.data.status === 402) {
          // 登录态失效, 重新登录
          App.doLogin(() => {
            App._post_form(url, data, success, fail);
          });
          return false;
        } else if (res.data.status === 500 || res.data.status === 422) {
          App.showError(res.data.message, () => {
            fail && fail(res);
          });
          return false;
        }
        success && success(res.data);
      },
      fail(res) {
        App.showError(res.message, () => {
          fail && fail(res);
        });
      },
      complete(res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        complete && complete(res);
      }
    });
  },

  /**
   * 验证是否存在user_info
   */
  validateUserInfo() {
    let user_info = wx.getStorageSync('user_info');
    return !!wx.getStorageSync('user_info');
  },

  /**
   * 对象转URL
   */
  urlEncode(data) {
    var _result = [];
    for (var key in data) {
      var value = data[key];
      if (value.constructor == Array) {
        value.forEach(_value => {
          _result.push(key + "=" + _value);
        });
      } else {
        _result.push(key + '=' + value);
      }
    }
    return _result.join('&');
  },

  /**
   * 设置当前页面标题
   */
  setTitle() {
    let App = this,
      wxapp;
    if (wxapp = wx.getStorageSync('wxapp')) {
      wx.setNavigationBarTitle({
        title: wxapp.wxapp_title
      });
    } else {
      App.getWxappBase(() => {
        App.setTitle();
      });
    }
  },

  /**
   * 设置navbar标题、颜色
   */
  setNavigationBar() {
    let App = this;
    // 获取小程序基础信息
    App.getWxappBase(wxapp => {
      // 设置navbar标题、颜色
      wx.setNavigationBarColor({
        frontColor: wxapp.top_text_color.text,
        // backgroundColor: wxapp.top_background_color
      })
    });
  },

  /**
   * 获取tabBar页面路径列表
   */
  getTabBarLinks() {
    return tabBarLinks;
  },
/**
 * 保存图片至手机相册
 */
 download(file, type) {
  return new Promise((resolve, reject) => {
    wx.downloadFile({
      url: file, // 资源的URL
      success: function(res) {
        if (res.statusCode === 200) {
          if (type !== 5) {
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath, // 下载图片的临时路径
              success: resolve,
              fail: reject
            });
          } else {
            wx.saveVideoToPhotosAlbum({
              filePath: res.tempFilePath, // 下载视频的临时路径
              success: resolve,
              fail: reject
            });
          }
        } else {
          reject(new Error('下载失败'));
        }
      },
      fail: function(err) {
        reject(new Error('下载失败', err));
      }
    });
  }).catch(err => {
    wx.showToast({
      title: '保存失败',
      icon: 'none'
    });
    console.error('保存失败', err);
  });
},

 /**
  * 图片预览
  */
 copyText(text) {
   const data = this.removeHTMLTags(text)
  wx.setClipboardData({
    data: data,
    success() {
      wx.showLoading({
        title: '下载中请稍等...'
      });
    },
    fail(err) {
      // wx.showToast({
      //   title: '复制失败',
      //   icon: 'none',
      //   duration: 2000
      // });
      console.error(err);
    }
  });
},
convertHtmlToText (inputText) {
  var returnText = "" + inputText;
  returnText = returnText.replace(/<\/p>/ig, '&#39;\r\n&#39');
  returnText = returnText.replace(/<\/li>/ig,'&#39;\r\n&#39');
  returnText = returnText.replace(/<li>/ig, '&#39; * &#39');
  returnText = returnText.replace(/<\/ul>/ig, '&#39;\r\n&#39');
  //-- remove BR tags and replace them with line break
  returnText = returnText.replace(/<br\s*[\/]?>/gi, "\r\n");

  //-- remove P and A tags but preserve what&#39;s inside of them
  returnText=returnText.replace(/<p.*?>/gi, "\r\n");
  returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 ($1)");

  //-- remove all inside SCRIPT and STYLE tags
  returnText=returnText.replace(/<script.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/script>/gi, "");
  returnText=returnText.replace(/<style.*>[\w\W]{1,}(.*?)[\w\W]{1,}<\/style>/gi, "");
  //-- remove all else
  returnText=returnText.replace(/<(?:.|\s)*?>/g, "");

  //-- get rid of more than 2 multiple line breaks:
  returnText=returnText.replace(/(?:(?:\r\n|\r|\n)\s*){2,}/gim, "\r\n\r\n");

  //-- get rid of more than 2 spaces:
  returnText = returnText.replace(/ +(?= )/g,'&#39;&#39');

  //-- get rid of html-encoded characters:
  returnText=returnText.replace(/ /gi," ");
  returnText=returnText.replace(/&/gi,"&");
  returnText=returnText.replace(/"/gi,'&#39;"&#39');
  returnText=returnText.replace(/</gi,'&#39;<&#39');
  returnText=returnText.replace(/>/gi,'&#39;>&#39');

  return returnText;
},
removeHTMLTags(html) {

     // 将 <br> 和 <p> 等标签替换为换行符
     html = html.replace(/<br\s*\/?>/gi, '\n');
     html = html.replace(/<\/p>/gi, '\n');
 
     // 去除所有其他 HTML 标签
     html = html.replace(/<[^>]+>/g, '');
 
     // 去除多余的换行符（可选）
     html = html.replace(/\n\s*\n/g, '\n');
     
  return html.trim()
}

})